<?php
namespace app\index\controller;

class User extends Common
{
    public function address()
    {
        $user_id = session('id');
        $address = db('address')->where('user_id',$user_id)->select();
        $shop = db('shopcar')->where('menber_id',$user_id)->select();
        $count = db('shopcar')->where('menber_id',$user_id)->count();
        $this->assign('count',$count);
        $this->assign('shop',$shop);
        $this->assign('address',$address);
        return view();
    }
    public function center()
    {
        $user_id = session('id');
        $userinfo = db('menber')->alias('a')->join('group b','a.group=b.id')->where('a.id',$user_id)->select();
        $shop = db('shopcar')->where('menber_id',$user_id)->select();
        $count = db('shopcar')->where('menber_id',$user_id)->count();
        $collect = db('collect')->where('menber_id',$user_id)->select();
        $ordersinfo = db('ordersinfo')->alias('a')->join('orders b','a.orders=b.orders')->where('user_id',$user_id)->select();
        $shopnum = db('ordersinfo')->where('user_id',$user_id)->count();
        $this->assign('ordersinfo',$ordersinfo);
        $this->assign('count',$count);
        $this->assign('shop',$shop);
        $this->assign('shopnum',$shopnum);
        $this->assign('userinfo',$userinfo);
        $this->assign('collect',$collect);
        return view();
    }
    public function message()
    {
        $user_id = session('id');
        $count = db('shopcar')->where('menber_id',$user_id)->count();
        $shop = db('shopcar')->where('menber_id',$user_id)->select();
        $message = db('shop_message')->alias('a')->join('shoper b','a.product_id=b.id')->where('user_id',$user_id)->select();
        $mess_count = db('shop_message')->alias('a')->join('shoper b','a.product_id=b.id')->where('user_id',$user_id)->count();
        $this->assign('count',$count);
        $this->assign('mess_count',$mess_count);
        $this->assign('shop',$shop);
        $this->assign('message',$message);
        return view();
    }
    public function delmess()
    {
        $mess_id = input('get.id');
        $res = db('shop_message')->where('mess_id',$mess_id)->delete();
        if ($res) {
            $this->success('删除成功',url('message'));
        }
    }
    public function collect()
    {
        $user_id = session('id');
        $collect = db('collect')->where('menber_id',$user_id)->select();
        $shop = db('shopcar')->where('menber_id',$user_id)->select();
        $count = db('shopcar')->where('menber_id',$user_id)->count();
        $this->assign('count',$count);
        $this->assign('shop',$shop);
        $this->assign('collect',$collect);
        return view();
    }
    public function delcollect()
    {
        $user_id = session('id');
        $shop_id = input('get.shop_id');
        $collect = db('collect')->where('menber_id',$user_id)->where('id',$shop_id)->delete();
        if ($collect) {
            echo '已删除该收藏商品';
        }
    }
    public function info()
    {
        $id = session('id');
        $user = db('menber')->where('id',$id)->select();
        foreach ($user as $key => $value) {
            $nickname = $value['nickname'];
            $phone = $value['phone'];
            $email = $value['email'];
        }
        $shop = db('shopcar')->where('menber_id',$id)->select();
        $count = db('shopcar')->where('menber_id',$id)->count();
        $this->assign('count',$count);
        $this->assign('shop',$shop);
        $this->assign('nickname',$nickname);
        $this->assign('phone',$phone);
        $this->assign('email',$email);
        return view();
    }
    public function integral()
    {
        $id = session('id');
        $shop = db('shopcar')->where('menber_id',$id)->select();
        $count = db('shopcar')->where('menber_id',$id)->count();
        $integral = db('orders')->where('menber_id',$id)->select();
        $userinfo = db('menber')->where('id',$id)->select();
        $this->assign('integral',$integral);
        $this->assign('userinfo',$userinfo);
        $this->assign('count',$count);
        $this->assign('shop',$shop);
        return view();
    }
    public function orders()
    {
        $id = session('id');
        $shop = db('shopcar')->where('menber_id',$id)->select();
        $count = db('shopcar')->where('menber_id',$id)->count();
        $orders = db('orders')->where('menber_id',$id)->select();
        $this->assign('orders',$orders);
        $this->assign('count',$count);
        $this->assign('shop',$shop);
        return view();
    }

    public function password()
    {
        $id = session('id');
        $user = db('menber')->where('id',$id)->select();
        foreach ($user as $key => $value) {
            $password = $value['pwd'];
        }
        $shop = db('shopcar')->where('menber_id',$id)->select();
        $count = db('shopcar')->where('menber_id',$id)->count();
        $this->assign('count',$count);
        $this->assign('shop',$shop);
        $this->assign('password',$password);
        return view();
    }
    public function changePassword()
    {
        $id = session('id');
        $post['repwd'] = input('post.repwd','','htmlspecialchars');
        $post['newpwd'] = input('post.newpwd','','htmlspecialchars');
        $data =[
            'pwd'=> md5($post['newpwd'])
        ];
        if ($post['newpwd']!=$post['repwd']) {
            $this->error('新密码与确认新密码不一致');
        }else {
            $user = db('menber')->where('id',$id)->update($data);
        }
        if ($user) {
            $this->success('修改成功',url('user/password'));
        }

    }
    public function changeUserInfo()
    {
        $id = session('id');
        $post['nickname'] = input('post.nickname','','htmlspecialchars');
        $post['phone'] = input('post.phone','','htmlspecialchars');
        $post['email'] = input('post.email','','htmlspecialchars');
        $data =[
            'nickname'=> $post['nickname'],
            'phone'=> $post['phone'],
            'email'=> $post['email'],
        ];
        $user = db('menber')->where('id',$id)->update($data);
        if ($user) {
            session('nickname',$post['nickname']);
            $this->success('修改成功',url('user/info'));
        }
    }
    public function insterAddress()
    {
        $user_id = session('id');
        $post['username'] = input('post.username','','htmlspecialchars');
        $post['area'] = input('post.province','','htmlspecialchars').input('post.city','','htmlspecialchars');
        $post['address'] = input('post.address','','htmlspecialchars');
        $post['postcodes'] = input('post.postcodes','','htmlspecialchars');
        $post['phone'] = input('post.phone','','htmlspecialchars');

        $data =[
            'username'=>$post['username'],
            'area'=>$post['area'],
            'address'=>$post['address'],
            'postcodes'=>$post['postcodes'],
            'phone'=>$post['phone'],
            'user_id'=>$user_id
        ];
        $address = db('address')->insert($data);
        if ($address) {
            $this->success('添加成功',url('user/address'));
        }
    }
    public function delAddress()
    {
        $user_id = input('get.id','','intval');
        $del = db('address')->delete($user_id);
        if ($del) {
            $this->success('删除成功',url('user/address'));
        }
    }
}